package com.cetc.sdp.kmga.cs.jdbc;

import com.cetc.sdp.kmga.cs.device.DeviceExpCode;
import com.cetc.sdp.kmga.cs.device.DeviceExpEvent;
import com.cetc.sdp.kmga.cs.device.StateLog;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author： DengQiang
 * @date: 2017/8/29 10:32
 */
public class DeviceDAO {

    public static List<DeviceExpCode> findAllDeviceExpCode() {
        List<DeviceExpCode> res = new ArrayList<>();
        String sql = "SELECT EXCP_CODE, TYPE, LEVEL, VALUE, CMP_TYPE FROM sdp_sys_device_excp_cd";
        SQLHelper.find(sql, rs -> {
            while (rs.next()) {
                DeviceExpCode expCode = new DeviceExpCode();
                expCode.setExcpCode(rs.getInt("EXCP_CODE"));
                expCode.setType(rs.getString("TYPE"));
                expCode.setLevel(rs.getInt("LEVEL"));
                expCode.setValue(rs.getDouble("VALUE"));
                expCode.setCmpType(rs.getInt("CMP_TYPE"));
                res.add(expCode);
            }
            return null;
        });
        return res;
    }

    public static Map<String, List<DeviceExpEvent>> findAllActiveExpEvent() {
        Map<String, List<DeviceExpEvent>> res = new HashMap<>();
        String sql = "SELECT ID, EXCP_CODE, DEVICE_ID, DETAIL, START_TIME, END_TIME FROM sdp_busi_device_excp_evt WHERE END_TIME IS NULL";
        SQLHelper.find(sql, rs -> {
            while (rs.next()) {
                DeviceExpEvent expEvent = new DeviceExpEvent();
                expEvent.setId(rs.getInt("ID"));
                expEvent.setExcpCode(rs.getInt("EXCP_CODE"));
                expEvent.setDeviceId(rs.getString("DEVICE_ID"));
                expEvent.setDetail(rs.getString("DETAIL"));
                expEvent.setStartTime(rs.getString("START_TIME"));

                List<DeviceExpEvent> list = res.computeIfAbsent(expEvent.getDeviceId(), k -> new ArrayList<>());
                list.add(expEvent);
            }
            return null;
        });
        return res;
    }

    public static List<DeviceExpEvent> findActiveExpEventByDevId(String devId) {
        List<DeviceExpEvent> res = new ArrayList<>();
        String sql = "SELECT ID, EXCP_CODE, DEVICE_ID, DETAIL, START_TIME, END_TIME FROM sdp_busi_device_excp_evt WHERE END_TIME IS NULL AND DEVICE_ID=?";
        SQLHelper.find(sql, ps -> {
            ps.setString(1, devId);
            return ps.executeQuery();
        }, rs -> {
            while (rs.next()) {
                DeviceExpEvent expEvent = new DeviceExpEvent();
                expEvent.setId(rs.getInt("ID"));
                expEvent.setExcpCode(rs.getInt("EXCP_CODE"));
                expEvent.setDeviceId(rs.getString("DEVICE_ID"));
                expEvent.setDetail(rs.getString("DETAIL"));
                expEvent.setStartTime(rs.getString("START_TIME"));
                res.add(expEvent);
            }
            return null;
        });
        return res;
    }

    public static List<DeviceExpEvent> findAllOfflineDevices() {
        List<DeviceExpEvent> offlineDevs = new ArrayList<>();
        String sql = "SELECT a.ID, a.EXCP_CODE, a.DEVICE_ID, a.DETAIL, a.START_TIME, a.END_TIME FROM sdp_busi_device_excp_evt AS a LEFT JOIN sdp_sys_device_excp_cd AS b ON a.EXCP_CODE=b.EXCP_CODE WHERE b.TYPE='离线' AND a.END_TIME IS NULL";
        SQLHelper.find(sql, rs -> {
            while (rs.next()) {
                DeviceExpEvent expEvent = new DeviceExpEvent();
                expEvent.setId(rs.getInt("ID"));
                expEvent.setExcpCode(rs.getInt("EXCP_CODE"));
                expEvent.setDeviceId(rs.getString("DEVICE_ID"));
                expEvent.setDetail(rs.getString("DETAIL"));
                expEvent.setStartTime(rs.getString("START_TIME"));
                offlineDevs.add(expEvent);
            }
            return null;
        });
        return offlineDevs;
    }

    public static int getOfflineExpCode() {
        return SQLHelper.find("SELECT EXCP_CODE FROM sdp_sys_device_excp_cd WHERE TYPE='离线'"
                , rs -> {
                    if (rs.next()) {
                        return rs.getInt(1);
                    } else {
                        return null;
                    }
                }).get();
    }

    public static int saveDeviceExpEvent(DeviceExpEvent expEvent) {
        String sql = "INSERT INTO sdp_busi_device_excp_evt(EXCP_CODE, DEVICE_ID, DETAIL, START_TIME, END_TIME) VALUES (?, ?, ?, ?, ?)";
        return SQLHelper.save(sql, ps -> {
            ps.setInt(1, expEvent.getExcpCode());
            ps.setString(2, expEvent.getDeviceId());
            ps.setString(3, expEvent.getDetail());
            ps.setString(4, expEvent.getStartTime());
            ps.setString(5, expEvent.getEndTime());
        });
    }

    public static int[] saveDeviceExpEvent(List<DeviceExpEvent> expEvents) {
        String sql = "INSERT INTO sdp_busi_device_excp_evt(EXCP_CODE, DEVICE_ID, DETAIL, START_TIME, END_TIME) VALUES (?, ?, ?, ?, ?)";
        return SQLHelper.saveBatch(sql, ps -> {
            for (DeviceExpEvent expEvent : expEvents) {
//                ps.setString(1, expEvent.getId());
                ps.setInt(1, expEvent.getExcpCode());
                ps.setString(2, expEvent.getDeviceId());
                ps.setString(3, expEvent.getDetail());
                ps.setString(4, expEvent.getStartTime());
                ps.setString(5, expEvent.getEndTime());
                ps.addBatch();
            }
        });
    }

    public static int updateDeviceExpEvent(DeviceExpEvent expEvent) {
        String sql = "UPDATE sdp_busi_device_excp_evt SET EXCP_CODE=?, DEVICE_ID=?, DETAIL=?, START_TIME=?, END_TIME=? WHERE ID = ?";
        return SQLHelper.save(sql, ps -> {
            ps.setInt(1, expEvent.getExcpCode());
            ps.setString(2, expEvent.getDeviceId());
            ps.setString(3, expEvent.getDetail());
            ps.setString(4, expEvent.getStartTime());
            ps.setString(5, expEvent.getEndTime());
            ps.setInt(6, expEvent.getId());
        });
    }

    public static int[] saveDeviceStateLog(List<StateLog> logs, String time) {
        String sql = "INSERT INTO sdp_busi_device_stat (DEVICE_ID, TIME, CPU_RATE, MEM_TOTAL, MEM_USED, MEM_BUFF, MEM_FREE, DISK_TOTAL, DISK_USED, DISK_FREE, NET_IN, NET_OUT, LOAD_AVG, TASK_TOTAL, TASK_RUN) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?, ?, ?)";
        return SQLHelper.saveBatch(sql, ps -> {
            for (StateLog log : logs) {
                ps.setObject(1, log.getDevId());
                ps.setObject(2, time);
                ps.setObject(3, log.getCpuUse());
                ps.setObject(4, log.getMemTotal());
                ps.setObject(5, log.getMemUse());
                ps.setObject(6, null);
                ps.setObject(7, null);
                ps.setObject(8, log.getDiskTotal());
                ps.setObject(9, log.getDiskUse());
                ps.setObject(10, null);
                ps.setObject(11, log.getNetRecv());
                ps.setObject(12, log.getNetSend());
                ps.setObject(13, null);
                ps.setObject(14, null);
                ps.setObject(15, null);
                ps.addBatch();
            }
        });
    }

    public static List<Tuple2<String, Integer>> findAllDevice() {
        List<Tuple2<String, Integer>> res = new ArrayList<>();
        String sql = "SELECT ID, status FROM sdp_busi_device_info WHERE deleted=0";
        SQLHelper.find(sql, rs -> {
            while (rs.next()) {
                res.add(new Tuple2<>(rs.getString(1), rs.getInt(2)));
            }
            return null;
        });
        return res;
    }

    public static void updateDeviceStatus(String devId, int stat) {
        String sql = "UPDATE sdp_busi_device_info SET STATUS=? WHERE id=?";
        SQLHelper.save(sql, ps -> {
            ps.setInt(1, stat);
            ps.setString(2, devId);
        });
    }
}
